package com.yxjx.duoxue.my;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Executors;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.datamodel.DMAppRef;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class AppRefActivity extends BaseActionBarActivity {
	
	private AppRefAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_app_ref);
		ListView mList = (ListView) findViewById(R.id.list);
		mAdapter = new AppRefAdapter();
		mList.setAdapter(mAdapter);
		customizeActionBarTitle("应用推荐");
		
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (mAdapter.getDataItem(arg2) != null) {
					String appPkg = mAdapter.getDataItem(arg2).getPackageName();
					if (Utils.isAppInstalled(getApplicationContext(), appPkg)) {
						Intent intent = AppRefActivity.this.getPackageManager()
								.getLaunchIntentForPackage(appPkg);
						startActivity(intent);
					} else {
						Utils.leadUserToAppMarket(getApplicationContext(), appPkg);
					}
				}
				
			}
		});
		
		LoadAppsAsyncTask task = new LoadAppsAsyncTask();
		task.execute("");
		
	}
	
	class AppRefAdapter extends BaseAdapter {

		private ArrayList<DMAppRef> mData;
		
		public DMAppRef getDataItem(int index) {
			if (mData == null || (index < 0) || index > mData.size()) {
				return null;
			}
			return mData.get(index);
		}
		public void setData(ArrayList<DMAppRef> data) {
			mData = data;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null ) {
				ret = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_app_ref, null);
			} else {
				ret = arg1;
			}
			UIHelper.setText(ret, R.id.title, mData.get(arg0).getTitle());
			UIHelper.setText(ret, R.id.subtilte, mData.get(arg0).getDesc());
			UIHelper.asyncLoadImage((ImageView) ret.findViewById(R.id.icon), mData.get(arg0).getIconUrl(), R.drawable.default_icon);
			
			return ret;
		}
			

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}
	}
	
	private void dismissLoadingDialog() {
		findViewById(R.id.list).setVisibility(View.VISIBLE);
		findViewById(R.id.loading).setVisibility(View.INVISIBLE);
	}

	class LoadAppsAsyncTask extends AsyncTask<String, Integer, ArrayList<DMAppRef>> {

		@Override
		protected ArrayList<DMAppRef> doInBackground(String... arg0) {
			ArrayList<DMAppRef> ret = null;
			try {
				ret = NwUtils.requestAllRefApps(getApplicationContext());
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return ret;
		}
		
		@Override
		protected void onPostExecute(ArrayList<DMAppRef> result) {
			if (result != null && result.size() != 0) {
				mAdapter.setData(result);
			}
			dismissLoadingDialog();
			super.onPostExecute(result);
		}
		
	}

}
